/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gerenciadorfinanceiro.controller.resources;

import gerenciadorfinanceiro.database.resources.Income;
import gerenciadorfinanceiro.database.resources.User;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.orm.jpa.support.JpaDaoSupport;

/**
 *
 * @author eduardo
 */
public class IncomeDAOImpl extends JpaDaoSupport implements IncomeDAO {

    public List getIncomes(User u, int month, int year) {
        Map<String, Object> m = new HashMap<String, Object>();
        m.put("owner", u);
        m.put("month", month);
        m.put("year", year);

        List l = this.getJpaTemplate().findByNamedQueryAndNamedParams("Income.findAllByUserMonth", m);

        return l;
    }

    public Income getIncomeById(int id) {
        Map<String, Object> m = new HashMap<String, Object>();
        m.put("id", id);

        List l = this.getJpaTemplate().findByNamedQueryAndNamedParams("Income.findById", m);

        if (l.size() == 1) {
            return (Income)l.get(0);
        } if (l.size() > 1) {
            return null;
        }else {
            return null;
        }
    }

    public void saveIncome(Income i) {
        this.getJpaTemplate().persist(i);
    }

    public void updateIncome(Income i) {
        this.getJpaTemplate().merge(i);
    }

}
